package fr.gest4team.app.pilotage.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.gest4team.app.pilotage.metier.ActeurOm;
import fr.gest4team.app.pilotage.metier.PlanningOm;
import fr.gest4team.app.pilotage.service.ServiceActeur;
import fr.gest4team.app.pilotage.service.ServicePlanning;

/**
 * Servlet implementation class PlanningServlet
 */
public class PlanningServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PlanningServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	private void execute(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		String dispatcher = "";
		final String action = request.getParameter("action");
		try {
			if ("saisie".equals(action)) {
				String codeActeur = request.getParameter("codeActeur");
				String semaine = request.getParameter("semaine");
				String annee = request.getParameter("annee");

				ActeurOm acteur = ServiceActeur.getInstance().chargerActeur(codeActeur);
				List<PlanningOm> plannings = ServicePlanning.getInstance().chargerListePlannings(acteur.getIdActeur());

				dispatcher = "/activites.jsp";
				request.setAttribute("SEMAINE", semaine);
				request.setAttribute("PLANNINGS", plannings);
				request.setAttribute("ACTEUR", acteur);
				request.setAttribute("ANNEE", annee);

			}

		} catch (final Exception e) {
			e.printStackTrace();
		}
		final RequestDispatcher rd = request.getRequestDispatcher(dispatcher);
		rd.forward(request, response);
	}

}
